package com.worthtech.billportal.dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

//import com.worthtech.billportal.bean.Module;
import com.worthtech.billportal.bean.CsCtrlGroupmodule;
import com.worthtech.billportal.bean.CsCtrlModule;
import com.worthtech.billportal.dao.ModuleDAO;

public class ModuleHibernateDAO extends HibernateDaoSupport implements
		ModuleDAO {
	private static final String LOADS_BY_PARENTID = "from CsCtrlModule where parentid = ?";

	public static final String[] FIND_MODULES = new String[3];

	static {
		FIND_MODULES[0] = " order by MODULEORDER";
		FIND_MODULES[1] = " order by mainPostNum desc";
		FIND_MODULES[2] = " order by postNum desc";
	}

	public List getModulesByParentId(int parentId) {
		return this.getHibernateTemplate().find(
				LOADS_BY_PARENTID + FIND_MODULES[0], new Long(parentId));
	}

	public CsCtrlModule getModule(int id) {
		return (CsCtrlModule) this.getHibernateTemplate().get(CsCtrlModule.class,
				new Integer(id));
	}

	public List getModuleMap(String usergroup) {
		return this.getHibernateTemplate().find(
				"from CsCtrlGroupmodule where groupid = ? order by moduleid",
				Long.parseLong(usergroup.trim()));
	}

	public List getModuleList(String cond) {
		return this.getSession().createQuery("from CsCtrlModule" + cond).list();
	}
	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if(perNo==0){
			List l = this.getHibernateTemplate().find(hql);
			System.out.println("size = "+l.size());
			return l;
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

}
